package com.pattern.practice.observer;

import java.util.ArrayList;
import java.util.List;

public class HealthData implements Subject {
    private int age;
    private int weight;
    private final List<Observer> observerList = new ArrayList<>();

    @Override
    public void registerObserver(Observer observer) {
        observerList.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        int index = observerList.indexOf(observer);
        if (index >= 0) {
            observerList.remove(index);
        }
    }

    private void notifyOfChange() {
        notifyObserver();
    }

    @Override
    public void notifyObserver() {
        for (Observer observer : observerList) {
            observer.update(age, weight);
        }
    }

    public void setHealthData(int age, int weight) {
        this.age = age;
        this.weight = weight;
        notifyOfChange();
    }
}
